package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.mapper.PermissionMapper;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.IPermissionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
public class PermissionServiceImpl implements IPermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private ApplicationContext ctx;
    // 既然能能拿到容器对象, 控制器对象也是存在里面的


    // 代码今天最难, 部分同学又会打击你, 又忘了好多东西
    @Override
    public void reload() {
        // 查询数据库中权限表中所有权限表达式
        List<String> expresssions = permissionMapper.selectAllExpression();

        // 获取容器中贴有 @Controller bean, 就是我们的控制器对象
        // key bean id 的值  value 控制器对象
        // departmentController    DepartmentController@123g1
        Map<String, Object> beanMap = ctx.getBeansWithAnnotation(Controller.class);
        // 获取控制器中处理的方法 反射
        Collection<Object> controllerBeans = beanMap.values();
        for (Object controllerBean : controllerBeans) {
            // 获取控制器类的字节码对象
            Class<?> controllerBeanClass = controllerBean.getClass().getSuperclass();
            // 获取本类的方法
            Method[] declaredMethods = controllerBeanClass.getDeclaredMethods();
            for (Method method : declaredMethods) {
                // 若方法贴有我们给自定义注解 @RequiredPermission
                if(method.isAnnotationPresent(RequiresPermissions.class)){ // 有就返回 true, 没有就返回 false
                    // System.out.println(method);
                    // 往数据库中 permission 表中插入数据
                    // 获取方法上注解
                    RequiresPermissions requiredPermissionAnno = method.getAnnotation(RequiresPermissions.class);
                    String name = requiredPermissionAnno.value()[1];
                    // String expression = StringUtil.getExpression(method);
                    String expression = requiredPermissionAnno.value()[0];

                    // 插入之前判断一下, 若这个权限在数据中存在, 那么不要插入
                    if(!expresssions.contains(expression)){ // 代表不存在
                        Permission permisison = new Permission();
                        permisison.setName(name);
                        permisison.setExpression(expression);
                        permissionMapper.insert(permisison);
                    }
                }

            }
        }

    }

    @Override
    public List<String> queryExpressionsByEmployeeId(Long employeeId) {
        return permissionMapper.selectExpressionsByEmployeeId(employeeId);
    }

    @Override
    public List<String> selectAllPermissions() {
        return permissionMapper.selectAllPermissions();
    }


    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Permission> list = permissionMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public void save(Permission permission) {
        permissionMapper.insert(permission);
    }

    @Override
    public void delete(Long id) {
        permissionMapper.deleteByRelation(id);
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Permission get(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Permission> list() {
        return permissionMapper.selectAll();
    }


}
